/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package at.tugraz.hidipic.web.execution.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import weka.classifiers.Evaluation;
import weka.core.Attribute;
import weka.core.Instances;

/**
 *
 * @author mario
 */
public class ExecutionUtil {
    /**
     * Method for creating a JIT-created pie chart for correct and incorrect classification values
     * @param eval the evaluation on the test set
     * @return the json string
     * @throws IOException 
     */
    public static String getJitJson(Evaluation eval) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        JsonFactory fac = new JsonFactory();
        JsonGenerator gen = fac.createJsonGenerator(output);
        gen.writeStartObject();
        Instances headers = eval.getHeader();
        Attribute attr = headers.classAttribute();
        gen.writeFieldName("values");
        gen.writeStartArray();
        gen.writeStartObject();
        gen.writeStringField("label", "Correct: " + eval.correct());
        gen.writeFieldName("values");
        gen.writeStartArray();
        gen.writeString(String.valueOf(eval.correct()));
        gen.writeEndArray();
        gen.writeEndObject();
        gen.writeStartObject();
        gen.writeStringField("label", "Incorrect: " + eval.incorrect());
        gen.writeFieldName("values");
        gen.writeStartArray();
        gen.writeString(String.valueOf(eval.incorrect()));
        gen.writeEndArray();
        gen.writeEndObject();
        gen.writeEndArray();
        gen.close();
        return output.toString();
    }
}
